var util = require('../../libs/util.js');
var app = getApp()
Page({
  data: {
    hasData: false,
    userInfo: {},
    truename: "",
    searchShowed: false,
    searchVal: "",
  },
  onShareAppMessage: function () {
    return {
      title: '鲜易派',
      path: '/pages/index/index'
    }
  },
  showSearch: function () {
    this.setData({
      searchShowed: true
    });
  },
  hideSearch: function () {
    this.setData({
      searchVal: "",
      searchShowed: false
    });
    this.loadData();
  },
  clearSearch: function () {
    this.setData({
      searchVal: ""
    });
    this.loadData();
  },
  searchTyping: function (e) {
    this.setData({
      searchVal: e.detail.value
    });
    this.loadData();
  },
  onLoad: function (e) {
    wx.setNavigationBarTitle({ "title": "鲜易派--平台调度端" });
    var that = this;
    this.setData({
      is_admin: app.globalData.admin == '1'?true:false
    })
    this.loadData();
  },
  loadData: function () {
    wx.showLoading({
      title: '加载数据中...',
    });
    var that = this;
    util.getData("driverManage", {
      CO_ID: app.globalData.CO_ID,
      sql: that.data.searchVal
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (res.data.success) {
        that.setData({
          hasData: true
        });
        var data = res.data.data, _data = [];
        for (var i = 0; i < data.length; i++) {
          _data.push({
            ID: data[i].ID,
            name: data[i].name,
            phone: data[i].phone,
            is_verify: (data[i].is_verify == '1' ? true : false),
            is_signing: (data[i].is_signing == '1' ? true : false),
            is_work: (data[i].is_work == '1' ? true : false)
          }); 
        }
        that.setData({
          driverData: _data
        })
      }
    });
  },
  setCompany:function(e){
    var id = e.currentTarget.id;
    wx.redirectTo({
      url: '/pages/fleet/driverOfCompany?type=driver&id=' + id
    })
  },
  deleteDriver: function (e) {
    var id = e.currentTarget.id;
    if (app.globalData.admin) {
      wx.showModal({
        title: '删除司机',
        content: '是否确认删除司机',
        success: function (res) {
          if (res.confirm) {
            wx.showLoading({
              title: '请稍候...',
            });
            util.putData("deleteDriver", {
              id: id,
              openid: app.globalData.openid
            }, function (res, error) {
              console.log(res); console.log(error)
              wx.hideLoading();
              if (error) {
                wx.showToast({
                  title: res.data.data,
                  image: '/images/wrong.png',
                  duration: 3000
                });
                return false;
              }
              if (res.data.success) {
                wx.showLoading({
                  title: "删除成功",
                  icon: 'success',
                  success: function () {
                    setTimeout(function () {
                      wx.redirectTo({
                        url: '/pages/fleet/driver'
                      })
                    }, 3000);
                  }
                });
              } else {
                wx.showToast({
                  title: res.data.data,
                  image: '/images/wrong.png',
                  duration: 3000
                });
                return false;
              }
            });
          } else if (res.cancel) {
          }
        }
      })
    }else{
      wx.showToast({
        title: "没有权限",
        image: '/images/wrong.png',
        duration: 3000
      });
    }
  },
  signingDriver: function (e) {
    var id = e.currentTarget.id;
    var driverData = this.data.driverData, is_signing = false, name = '';
    for (var i = 0; i < driverData.length; i++) {
      if (driverData[i].ID == id && driverData[i].is_signing == true) {
        is_signing = true;
        name = driverData[i].name;
        break;
      }
    };
    if (is_signing) {
      wx.showModal({
        title: '司机签约',
        content: '司机（' + name + '）已签约',
        showCancel: false,
        success: function (res) {
        }
      })
      return false;
    } else {
      wx.showModal({
        title: '司机签约',
        content: '是否确认签约司机',
        success: function (res) {
          if (res.confirm) {
            wx.showLoading({
              title: '请稍候...',
            });
            util.putData("signingDriver", {
              id: id
            }, function (res, error) {
              console.log(res); console.log(error)
              wx.hideLoading();
              if (error) {
                wx.showToast({
                  title: '签约失败',
                  image: '/images/wrong.png',
                  duration: 3000
                });
                return false;
              }
              if (res.data.success) {
                wx.showLoading({
                  title: "签约通过",
                  icon: 'success',
                  success: function () {
                    setTimeout(function () {
                      wx.redirectTo({
                        url: '/pages/fleet/driver'
                      })
                    }, 3000);
                  }
                });
              } else {
                wx.showToast({
                  title: '签约失败',
                  image: '/images/wrong.png',
                  duration: 3000
                });
                return false;
              }
            });
          } else if (res.cancel) {
          }
        }
      })
    }
  },
  verifyDriver: function (e) {
    var id = e.currentTarget.id;
    var driverData = this.data.driverData, is_verify = false, name = '';
    for (var i = 0; i < driverData.length; i++) {
      if (driverData[i].ID == id && driverData[i].is_verify == true) {
        is_verify = true;
        name = driverData[i].name;
        break;
      }
    };
    if (is_verify) {
      wx.showModal({
        title: '司机审核',
        content: '司机（' + name + '）已审核',
        showCancel: false,
        success: function (res) {
        }
      })
      return false;
    } else {
      wx.showModal({
        title: '司机审核',
        content: '是否确认司机审核通过',
        success: function (res) {
          if (res.confirm) {
            wx.showLoading({
              title: '请稍候...',
            });
            util.putData("verifyDriver", {
              id: id
            }, function (res, error) {
              console.log(res); console.log(error)
              wx.hideLoading();
              if (error) {
                wx.showToast({
                  title: '审核失败',
                  image: '/images/wrong.png',
                  duration: 3000
                });
                return false;
              }
              if (res.data.success) {
                wx.showLoading({
                  title: "审核通过",
                  icon: 'success',
                  success: function () {
                    setTimeout(function () {
                      wx.redirectTo({
                        url: '/pages/fleet/driver'
                      })
                    }, 3000);
                  }
                });
              } else {
                wx.showToast({
                  title: '审核失败',
                  image: '/images/wrong.png',
                  duration: 3000
                });
                return false;
              }
            });
          } else if (res.cancel) {
          }
        }
      })
    }
  },
  callDriver: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone
    })
  },
  gotoHome: function () {
    wx.redirectTo({
      url: '/pages/fleet/fleet'
    })
  },
  gotoVehicle: function () {
    wx.redirectTo({
      url: '/pages/fleet/vehicle'
    })
  },
  gotoDriver: function () {
    wx.redirectTo({
      url: '/pages/fleet/driver'
    })
  },
  gotoInfo: function () {
    wx.redirectTo({
      url: '/pages/fleet/apps'
    })
  }
})
